package com.mall.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.system.entity.UserRole;
import com.mall.system.mapper.UserRoleMapper;
import com.mall.system.service.UserRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户角色关联服务实现类
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {
    
    @Override
    public List<Long> selectRoleIdsByUserId(Long userId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<UserRole> userRoles = this.list(queryWrapper);
        return userRoles.stream()
                .map(UserRole::getRoleId)
                .collect(Collectors.toList());
    }
    
    @Override
    public List<Long> selectUserIdsByRoleId(Long roleId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", roleId);
        List<UserRole> userRoles = this.list(queryWrapper);
        return userRoles.stream()
                .map(UserRole::getUserId)
                .collect(Collectors.toList());
    }
    
    @Override
    public Set<Long> selectRoleIdsByUserIds(List<Long> userIds) {
        if (userIds == null || userIds.isEmpty()) {
            return Collections.emptySet();
        }
        
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIds);
        List<UserRole> userRoles = this.list(queryWrapper);
        return userRoles.stream()
                .map(UserRole::getRoleId)
                .collect(Collectors.toSet());
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateUserRole(Long userId, List<Long> roleIds) {
        // 删除用户原有的角色关联
        QueryWrapper<UserRole> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("user_id", userId);
        this.remove(deleteWrapper);
        
        // 新增用户角色关联
        if (roleIds != null && !roleIds.isEmpty()) {
            List<UserRole> userRoles = roleIds.stream()
                    .map(roleId -> {
                        UserRole userRole = new UserRole();
                        userRole.setUserId(userId);
                        userRole.setRoleId(roleId);
                        return userRole;
                    })
                    .collect(Collectors.toList());
            this.saveBatch(userRoles);
        }
        
        return true;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateUserRole(List<Long> userIds, Long roleId) {
        if (userIds == null || userIds.isEmpty()) {
            return false;
        }
        
        // 删除用户原有的角色关联
        QueryWrapper<UserRole> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.in("user_id", userIds);
        this.remove(deleteWrapper);
        
        // 新增用户角色关联
        List<UserRole> userRoles = userIds.stream()
                .map(userId -> {
                    UserRole userRole = new UserRole();
                    userRole.setUserId(userId);
                    userRole.setRoleId(roleId);
                    return userRole;
                })
                .collect(Collectors.toList());
        this.saveBatch(userRoles);
        
        return true;
    }
}